//
//  PPSettingManager.m
//  pickpic
//
//  Created by GaoYongyue on 16/10/2.
//  Copyright © 2016年 MicroDream. All rights reserved.
//

#import "PPSettingManager.h"
#import "NSString+JsonKit.h"

@implementation PPSettingManager
+ (instancetype)sharedInstance {
    static id instance;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        instance = self.new;
    });

    return instance;
}

- (instancetype)init {
    self = [super init];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"region" ofType:@"json"];
    NSString *fileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    NSArray *regions = (NSArray *)[fileContents cd_objectFromJSONString];

    NSMutableArray *provinces = [[NSMutableArray alloc] init];
    [regions enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([[[obj validDictionary] validNumberForKey:@"level"] integerValue] == 1)
        {
            [provinces addSafeObject:obj];
        }
    }];
    self.provinceArray = [NSArray arrayWithArray:provinces];

    NSMutableArray *cityArray = [[NSMutableArray alloc] init];
    [self.provinceArray enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSInteger sheng = [[[obj validDictionary] validNumberForKey:@"sheng"] integerValue];
        NSMutableArray *citys = [[NSMutableArray alloc] init];
        [regions enumerateObjectsUsingBlock:^(NSDictionary *obj1, NSUInteger idx, BOOL * _Nonnull stop) {
            NSInteger sheng1 = [[[obj1 validDictionary] validNumberForKey:@"sheng"] integerValue];
            NSInteger level1 = [[[obj1 validDictionary] validNumberForKey:@"level"] integerValue];
            if (sheng1 == sheng && level1 == 2)
            {
                [citys addSafeObject:obj1];
            }
        }];
        [cityArray addSafeObject:citys];
    }];

    self.cityArray = [NSArray arrayWithArray:cityArray];
    return self;
}
@end
